﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Alimentador.Validations
{
    /// <summary>
    /// Interaction logic for ValidateCtrl.xaml
    /// </summary>
    public partial class ValidateCtrl : Window
    {
        private const int MIN_NUMBER_SIZE = 9;

        public delegate void ResponseCodeEventDelegate(PApwd responseCode);
        public event ResponseCodeEventDelegate ResponseCodeEvent;
        
        public ValidateCtrl()
        {
            InitializeComponent();

            ContinueBtn.IsEnabled = false;
            NumbertxtBox.Focus();
        }

        private void ContinueBtnClick(object sender, RoutedEventArgs e)
        {
            if (ResponseCodeEvent != null)
            {
                PApwd pw = new PApwd();
                pw.enterCode = ResponsetxtBox.Text.ToUpper();
                pw.studentNumber = NumbertxtBox.Text.ToUpper();

                ResponseCodeEvent(pw);
            }

            this.Close();
        }

        private void NumbertxtBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            ContinueBtn.IsEnabled = (ResponsetxtBox.Text.Length > 0 && NumbertxtBox.Text.Length >= MIN_NUMBER_SIZE);
        }

        private void ResponsetxtBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            ContinueBtn.IsEnabled = (ResponsetxtBox.Text.Length > 0 && NumbertxtBox.Text.Length >= MIN_NUMBER_SIZE);
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void ResponsetxtBox_LostFocus(object sender, RoutedEventArgs e)
        {
            ResponsetxtBox.Text = ResponsetxtBox.Text.ToUpper();
        }
    }
}
